<?php
/*
 *A view helper welcome message
 */

class My_View_Helper_LoggedInUser {
	
	protected $view;
	
	//saves view object
	function setView($view) {
		$this->view = $view;
	}
	
	function loggedInUser() {
		//get auth instance
		$auth = Zend_Auth::getInstance ();
		
		// db initialized in My_Plugin_Initializer
		$db = Zend_Registry::get ( 'db' );
		
		//set db fetchmode
		$db->setFetchMode ( Zend_Db::FETCH_OBJ );
		
		//return var 
		$string = '';
		
		//user is logged in
		if ($auth->hasIdentity ()) {
			$logoutUrl = $this->view->url ( array ('controller' => 'auth', 'action' => 'logout' ) );
			
			//gets user record
			$user = $auth->getIdentity ();
			$userId = ( int ) Zend_Auth::getInstance ()->getIdentity ()->id;
			
			$username = $this->view->escape ( $user->username );
			$user_role = Zend_Auth::getInstance ()->getIdentity ()->role;
			$first_name = $this->view->escape ( ucfirst ( $user->first_name ) );
			$last_name = $this->view->escape ( ucfirst ( $user->last_name ) );
			$date_created = $user->date_created;
			$date_updated = $user->date_updated;
			
			$string .= $username . ' (' . $user_role . ') | <a href="' . $logoutUrl . '">Log out</a>';
		} else {
			
			$request = Zend_Controller_Front::getInstance ()->getRequest ();
			
			//get ip
			$user_ip = $request->getServer ( 'REMOTE_ADDR' );
			
			//if it's not empty check to see if it's registered as a guest user
			if (! empty ( $user_ip )) {
				
				$result = $db->fetchRow ( 'SELECT * FROM users WHERE user_ip = ?', $user_ip );
				
				if ($result->user_ip) {
					
					//get guest record of existing guest user
					$user_id = $result->id;
					$username = $this->view->escape ( $result->username );
					$first_name = $this->view->escape ( $result->first_name );
					$last_name = $this->view->escape ( $result->last_name );
					$user_role = $this->view->escape ( $result->role );
					$date_created = $result->date_created;
					$date_updated = $result->date_updated;
					
					//update guest user
					$data = array ('date_updated' => new Zend_Db_Expr ( 'NOW()' ) );
					
					$db->update ( 'users', $data, "id = {$result->id}" );
				
				} else {
					// user_ip NOT in the db i.e. new visitor
					
					//insert as guest user
					$data = array ('date_created' => new Zend_Db_Expr ( 'NOW()' ), 'username' => 'StockHeadline Guest', 'first_name' => 'Guest', 'last_name' => 'User', 'user_ip' => $user_ip, 'role' => 'guest' );
					
					$db->insert ( 'users', $data );
					
					// return the last value generated by an auto-increment column
					//set as new user id
					$id = $db->lastInsertId ();
					
					//fetch row of newly created user
					$result = $db->fetchRow ( 'SELECT * FROM users WHERE id = ?', $id );
					
					//set new user metadata
					$user_id = $result->id;
					$username = $this->view->escape ( $result->username );
					$first_name = $this->view->escape ( $result->first_name );
					$last_name = $this->view->escape ( $result->last_name );
					$user_role = $this->view->escape ( $result->role );
					$date_created = $result->date_created;
					$date_updated = $result->date_updated;
					
				}
			
			}
			
			$loginUrl = $this->view->url ( array ('controller' => 'auth', 'action' => 'login' ) );
			$registerUrl = $this->view->url ( array ('controller' => 'auth', 'action' => 'register' ) );
			
			$strLogin = '<a href="' . $loginUrl . '">Log in</a>';
			$strRegister = '<a href="' . $registerUrl . '">Register</a>';
			
			$string .= $username . ' ' . $strLogin . ' | ' . $strRegister;
		
		}
		
		//$string .= "<div>Role: {$user_role}</div>";
		//$string .= "<div>Last update: {$date_updated}</div>";
		
		return $string;
	}
	
//close class
}